using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace UselessExplorer
{
    public partial class frmAllowedExtensions : Form
    {
        public frmAllowedExtensions()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            clsGlobal cg = clsGlobal.Instance;
            int i = 0;
            string[] temp = new string [listBox1.Items.Count];
            foreach (string item in listBox1.Items)
            {
                temp[i] = item;
                i++;
            }
            cg.Extenstions = temp;
            toolStripStatusLabel1.Text = (cg.SaveExtensions() == true) ? "Extension added successfully" :  "Unable to Add the Extension / it is restricted";
        }

        private void frmRestrictedExtensions_Load(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "";
            clsGlobal cg = clsGlobal.Instance;
            bool flag = true;
            
            this.Left = cg.FormLeft;
            this.Top = cg.FormTop;

            cg.Extenstions = readConfiguration();
            
            if (cg.Extenstions != null)
            {
                string[] temp = cg.Extenstions;
                foreach (string item in temp)
                {

                    for (int i = 1; i <= 8; i++)
                    {
                        System.Windows.Forms.Control[] c = this.Controls.Find("CheckBox" + i, true);
                        CheckBox cb = (CheckBox)c[0];

                        if (item == cb.Text)
                        {
                            cb.Checked = true;
                            listBox1.Items.Add(item);
                            flag = true;
                            break;
                        }
                        else
                            flag = false;
                    }
                    if (!flag)
                        listBox1.Items.Add(item);
                
                }

                
            }
        }

        public static string[] readConfiguration()
        {
            int totalKeys = ConfigurationManager.AppSettings.Keys.Count;
            string[] keys   = new string[totalKeys];
            string[] values = new string[totalKeys];
            string[] temp = new string[1];

            keys = ConfigurationManager.AppSettings.AllKeys;
            
            for (int i = 0; i < keys.Length; i++)
            {
                temp = (ConfigurationManager.AppSettings.GetValues(keys[i]));
                foreach (string keyValue in temp)
                    values[i] = keyValue;
            }
            return values;
        }

        /// <summary>
        /// Save the Configuration for the Extensions Added. Returns true if added successfully.
        /// </summary>
        /// 
        public bool SaveConfiguration()
        {
            string[] extensions = clsGlobal.Instance.Extenstions;
            string keyword = "ext";
            int totalKeys = 0;
            bool restrictedKey = false;
            bool success = false;

            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            totalKeys = ConfigurationManager.AppSettings.Keys.Count;

            config.AppSettings.Settings.Clear();
            
            for (int i = 0; i < extensions.Length; i++)
            {
                restrictedKey = isRestricted(extensions[i]);
                //keyAvailable = isDuplicate(extensions[i]);

                if (!restrictedKey)
                {
                    totalKeys = ConfigurationManager.AppSettings.Keys.Count;
                    config.AppSettings.Settings.Add(keyword + (totalKeys), extensions[i]);
                    config.Save(ConfigurationSaveMode.Modified);
                    ConfigurationManager.RefreshSection("appSettings");
                    success = true;
                }
                else
                {
                    success = false;
                }
            }

            return success;
        }
        
        public bool isRestricted(string extension)
        {
            string[] restrictedExtensions = { ".exe", ".dll", ".ico" };
            bool restricted = false;

            foreach (string ext in restrictedExtensions)
            {
                if (ext == extension)
                {
                    restricted = true;
                    break;
                }
                
            }
            return restricted;
        }

        private bool isDuplicate(string extension)
        {
            int totalKeys = ConfigurationManager.AppSettings.Keys.Count;
            string keyword = "ext";
            string[] temp = null;
            bool isAvailable = false;

            for (int i = 1; i <= totalKeys; i++)
            {
                temp = ConfigurationManager.AppSettings.GetValues(keyword + i);

                if (temp[0] == extension)
                {
                    isAvailable = true;
                    break;
                }
                else
                    isAvailable = false;
            }
            return isAvailable;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            for (int i = 1; i <= 8; i++)
            {
                bool flag = false;
                System.Windows.Forms.Control[] c= this.Controls.Find("CheckBox"+i,true);
                CheckBox cb = (CheckBox)c[0];
                
                if (cb.Checked)
                {
                    
                    if (listBox1.Items.Count == 0)
                    {
                        listBox1.Items.Add(cb.Text.ToLower());
                    }
                    else
                    {
                        foreach (string item in listBox1.Items)
                        {
                            if (item == cb.Text)
                            {
                                flag = true;
                                break;
                            }
                        }

                        if (!flag)
                            listBox1.Items.Add(cb.Text.ToLower());
                    }
                }
                else if(cb.Checked == false)
                {
                    for ( int j = 0; j < listBox1.Items.Count; j++ )
                    {

                        if (listBox1.Items[j].ToString() == cb.Text)
                            listBox1.Items.RemoveAt(j);
                    }
                }

            }
            bool flag2 = true;
            if (textBox1.Text != "")
            {
                foreach (string item in listBox1.Items)
                {
                    if (textBox1.Text == item)
                    {
                        flag2 = true;
                        break;
                    }
                    else
                        flag2 = false;
                }
            }

            if (!flag2)
            {
                listBox1.Items.Add(textBox1.Text);
                textBox1.Text = "";
            }
        }
    }
}